<template>
  <div class="myCategoryBanner center">
    <div
      class="swiper"
      @mouseenter="mouseenterChange(true)"
      @mouseleave="mouseenterChange(false)"
    >
      <transition-group name="opcatiy">
        <img
          :src="item.imgUrl"
          v-for="(item, index) in banner"
          :key="index"
          v-show="imgCurrentIndex == index"
        />
      </transition-group>
      <transition-group name="opcatiy">
        <span
          class="iconfont icon-xiangzuojiantou left"
          @click="currentIndexHandle(-1)"
          v-if="hasDirector"
        ></span>
        <span
          class="right iconfont icon-xiangyoujiantou"
          @click="currentIndexHandle(1)"
          v-if="hasDirector"
        ></span>
      </transition-group>
      <ul class="ul">
        <li
          v-for="(item, index) in banner.length"
          :key="item"
          :class="index == imgCurrentIndex ? 'active' : ''"
          @click="imgCurrentIndex = index"
        ></li>
      </ul>
    </div>
  </div>
</template>
<script>
/* 引入轮播图数据 */
import { getBanner } from "@/api/home/banner";
import {
  ref,
  reactive,
  toRefs,
  computed,
  onMounted,
  onBeforeUnmount,
} from "vue";
import { useRouter, useRoute } from "vue-router";
import { useStore } from "vuex";
import Carousel from "@/components/communal/swiper/index";
import CarItem from "@/components/communal/swiper/item";
export default {
  props: {},
  name: "myCategoryBanner",
  setup(props, ctx) {
    /* 定义仓库 */
    const store = useStore();
    /* 定义路由传参 */
    const router = useRouter();
    /* 定义路由收参 */
    const route = useRoute();
    /* 定义总数据存放地址 */
    const data = reactive({
      banner: [],
    });
    getBanner().then((res) => {
      // console.log(res);
      data.banner = res.result;
    });
    const swiper = reactive({
      imgCurrentIndex: 0,
      time: null,
      hasDirector: false,
      autopaly: () => {
        swiper.time = setInterval(() => {
          swiper.imgCurrentIndex += 1;
          if (swiper.imgCurrentIndex >= data.banner.length) {
            swiper.imgCurrentIndex = 0;
          }
        }, 3000);
      },
      currentIndexHandle: (i) => {
        swiper.imgCurrentIndex += i;
        if (swiper.imgCurrentIndex === data.banner.length) {
          swiper.imgCurrentIndex = 0;
        }
        if (swiper.imgCurrentIndex === -1) {
          swiper.imgCurrentIndex = data.banner.length - 1;
        }
      },
      mouseenterChange: (bool) => {
        if (bool) {
          clearInterval(swiper.time);
          swiper.time = null;
        } else {
          swiper.autopaly();
        }
        swiper.hasDirector = bool;
      },
    });
    onMounted(() => {
      swiper.autopaly();
    });
    onBeforeUnmount(() => {
      clearInterval(swiper.time);
      swiper.time = null;
    });
    return { ...toRefs(data),...toRefs(swiper) };
  },
  components: { Carousel, CarItem },
};
</script>
<style lang="scss" scoped>
/* 如果不想样式污染  就加个scoped */
.myCategoryBanner {
  width: 1240px;
  height: 500px;
  margin: 0 auto;
  margin-bottom: 20px;
  .swiper {
    width: 100%;
    height: 100%;
    position: relative;
    cursor: pointer;
    img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }
    span {
      z-index: 9;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      z-index: 99999;
      font-size: 20px;
      width: 40px;
      height: 40px;
      border-radius: 50%;
      background-color: #aaa;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      color: #fff;
      &.left {
        left: 20px;
      }
      &.right {
        right: 20px;
      }
    }
    .ul {
      position: absolute;
      bottom: 30px;
      left: 50%;
      display: flex;
      transform: translateX(-50%);
      li {
        width: 12px;
        height: 12px;
        border-radius: 50%;
        background-color: rgba(0, 0, 0, 0.507);
        margin-left: 12px;
        cursor: pointer;
        transition: all 0.3s;
        &.active {
          background-color: #fff;
        }
        &:hover {
          background-color: var(--color);
          transform: scale(1.5);
        }
      }
    }
  }
}
.opcatiy-enter-active,
.opcatiy-leave-active {
  transition: opacity 0.3s linear;
}
/* 定义进入过渡生效时的状态 */
.opcatiy-enter-active {
  opacity: 0;
}
/* 定义进入过渡的结束状态 */
.opcatiy-enter-to {
  opacity: 1;
}
/* 定义离开过渡的开始状态 */
.opcatiy-leave-active {
  opacity: 1;
}
/* 定义离开过渡的结束状态 */
.opcatiy-leave-to {
  opacity: 0;
}
</style>